<template>
  <!-- 新增活动申报 -->
  <view class="newActDeclaration">
    <view class="wrapper">
      <view class="wrapper-item content">
        <u-form :model="form" ref="form" label-width="150" labelPosition="’top‘" :error-type="errorType">
          <u-form-item label="选择派出所" :borderBottom="false" :required="true" @click="policeAgencyFoces">
            <u--input v-model="form.policeAgencyName" disabled disabledColor="#ffffff" placeholder="请选择活动举办地所属派出所"
              border="none"></u--input>
            <u-icon slot="right" name="arrow-right"></u-icon>
          </u-form-item>

          <u-form-item label="活动名称" :borderBottom="false" :required="true">
            <u--input v-model.trim="form.activityName" border="none" placeholder="请填写" :maxlength="30"></u--input>
          </u-form-item>

          <u-form-item label="申报人员" :borderBottom="false" :required="true">
            <u--input v-model.trim="form.declarationPersonnel" border="none" placeholder="请填写"
              :maxlength="30"></u--input>
          </u-form-item>

          <u-form-item label="联系方式" :borderBottom="false" :required="true">
            <u--input v-model.trim="form.mobile" border="none" placeholder="请填写" :maxlength="11"
              @blur="phoneHandle"></u--input>
          </u-form-item>

          <u-form-item label="举办单位" :borderBottom="false" :required="true">
            <u--input v-model.trim="form.hostUnit" border="none" placeholder="请填写" :maxlength="30"></u--input>
          </u-form-item>

          <!-- ============================ -->
          <u-form-item label="活动开始时间" :borderBottom="false" :required="true" @click="cellTimeClick('beginTimeName')">
            <u--input v-model="form.beginTimeName" disabled disabledColor="#ffffff" placeholder="请选择活动开始时间"
              border="none"></u--input>
            <image :src="baseImgUrl + 'day-icon2.png'" mode="" slot="right" class="seleTime"></image>
          </u-form-item>

          <u-form-item label="活动结束时间" :borderBottom="false" :required="true" @click="cellTimeClick('endTimeName')">
            <u--input v-model="form.endTimeName" disabled disabledColor="#ffffff" placeholder="请选择活动开始时间"
              border="none"></u--input>
            <image :src="baseImgUrl + 'day-icon2.png'" mode="" slot="right" class="seleTime"></image>
          </u-form-item>
          <!-- ============================ -->

          <u-form-item label="活动地点" :borderBottom="false" :required="true">
            <view class="map-form">
              <u--input v-model="activityLocation" border="none" placeholder="在地图上选择活动举办地点" :maxlength="30"
                :readonly="true"></u--input>
              <view class="map-img" @click="mapHandle">
                <image :src="baseImgUrl + 'map-icon2.png'" mode="aspectFill"></image>
              </view>
            </view>
          </u-form-item>

          <u-form-item label="活动对象" :borderBottom="false" :required="true">
            <u--input v-model.trim="form.activityObject" border="none" placeholder="请填写参加活动的人群"
              :maxlength="30"></u--input>
          </u-form-item>

          <u-form-item label="预计参加活动人数" :borderBottom="false" :required="true">
            <u--input v-model.trim="form.activityNumber" border="none" placeholder="请填写" :maxlength="5"
              @blur="actnumBlur"></u--input>
          </u-form-item>

          <view style="padding-top: 20rpx"></view>
          <view class="forTitle">
            <view> <text class="forReq">*</text>活动内容 </view>
          </view>
          <view class="text-AreaView">
            <u--textarea v-model="activityContent" placeholder="请用简洁的文字描述您要申办的活动内容" count maxlength="300"
              height="135"></u--textarea>
          </view>
        </u-form>
      </view>
    </view>

    <view class="btns thispageBtn">
      <button class="cancel-btn lt" type="primary" @click="navBack()">
        取消
      </button>
      <button class="submit-btn" type="primary" @click="submitHandle">
        保存
      </button>
    </view>

    <!-- 时间选择器 -->
    <u-datetime-picker ref="datetimePicker" :show="datetimeShow" v-model="timeValue" mode="datetime"
      :formatter="formatter" :minDate="minDate" :maxDate="maxDate" @close="pickerTimeClose" @cancel="pickerTimeClose(1)"
      @confirm="showTimeConfirm" :closeOnClickOverlay="true"></u-datetime-picker>
  </view>
</template>

<script>
  import {
    activityDeclarationSave
  } from "@/api/myself.js";
  import {
    Toast,
    navBack,
    handleClickRouter,
    uniTrim,
    pvwImage,
    useNetWork,
    isLoading,
    formatters,
    dayTurnTime,
    isNumber,
    getLocation,
  } from "@/utils/util.js";
  import moment from "@/utils/moment.js";
  import mixin from "@/utils/mixin.js";

  export default {
    name: "newActDeclaration",
    mixins: [mixin],
    data() {
      return {
        baseImgUrl: this.$baseImgUrl,
        baseURL: this.$baseURL,
        imgPrefix: uni.getStorageSync("imgPrefix"),
        options: {},
        form: {},
        errorType: ["none"],
        activityContent: "",
        activityLocation: "",
        lnglat: {}
      };
    },
    async onLoad(options) {
      this.options = options;
      uni.showLoading({
        title: '正在获取当前位置信息，请稍等！'
      })
      let location = await getLocation()
      uni.hideLoading()
      let lnglat = {
        lng: location.longitude,
        lat: location.latitude
      }
      this.lnglat = lnglat
    },
    onShow() {
      uni.$on("leadPoliceStation", (v) => {
        let {
          policeAgencyName,
          id
        } = v[0];
        this.$set(this.form, "policeAgencyId", id);
        this.$set(this.form, "policeAgencyName", policeAgencyName);
        this.form = Object.assign({}, this.form);
      });
    },
    onUnload() {
      uni.$off("leadPoliceStation");
    },
    methods: {
      handleClickRouter,
      navBack,
      pvwImage,
      moment,
      policeAgencyFoces() {
        uni.navigateTo({
          url: `/leadReporting/leadReporting/leadPoliceStation?lng=${this.lnglat.lng}&lat=${this.lnglat.lat}`,
        });
      },
      phoneHandle() {
        if (this.form.mobile && !this.$u.test.mobile(this.form.mobile)) {
          Toast("请输入正确的手机号");
          return;
        }
      },
      actnumBlur() {
        let {
          activityNumber
        } = this.form;
        if (activityNumber && !isNumber(activityNumber)) {
          Toast("活动人数请输入正整数");
          return;
        }
      },
      formatter(type, value) {
        if (type === "year") {
          return `${value}年`;
        }
        if (type === "month") {
          return `${value}月`;
        }
        if (type === "day") {
          return `${value}日`;
        }
        return value;
      },
      cellTimeClick(e) {
        this.cellType = e;
        var minDateNum = +new Date(); //获取当前时间的毫秒数
        this.maxDate = new Date(2099, 0, 1).getTime();

        switch (this.cellType) {
          case "beginTimeName":
            this.minDate = minDateNum;

            this.$nextTick(() => {
              this.timeValue = this.minDate;
            });
            break;
          case "endTimeName":
            this.minDate = dayTurnTime(this.form.beginTimeName);

            this.$nextTick(() => {
              this.timeValue = this.minDate;
            });
            break;
          default:
            break;
        }

        if (this.cellType == "endTimeName" && !this.form.beginTimeName) {
          Toast("请选择活动开始时间");
          return false;
        }

        setTimeout(() => {
          this.datetimeShow = true;
        }, 100);
      },
      pickerTimeClose(e) {
        if (e) {
          switch (this.cellType) {
            case "beginTimeName":
              this.form.beginTime = "";
              this.form.beginTimeName = "";
              break;
            case "endTimeName":
              this.form.endTime = "";
              this.form.endTimeName = "";
              break;
            default:
              break;
          }
        }
        this.datetimeShow = false;
      },
      /* 时间选择器 确定 */
      showTimeConfirm(e) {
        let arr = e.value;
        if (arr) {
          switch (this.cellType) {
            case "beginTimeName":
              this.form.beginTimeName = formatters(arr, "YY/MM/DD HH:mm");
              this.form.beginTime = dayTurnTime(this.form.beginTimeName);

              this.form.endTime = "";
              this.form.endTimeName = "";
              break;
            case "endTimeName":
              this.form.endTimeName = formatters(arr, "YY/MM/DD HH:mm");
              this.form.endTime = dayTurnTime(this.form.endTimeName);
              break;
            default:
              break;
          }
        }
        this.datetimeShow = false;
      },
      mapHandle() {
        let _this = this;
        uni.chooseLocation({
          success: function(res) {
            let {
              longitude,
              latitude,
              address
            } = res;
            _this.form.lng = longitude;
            _this.form.lat = latitude;
            _this.activityLocation = address;
          },
          fail: function(err) {
            console.log(err, "错误的");
          },
        });
      },
      submitHandle() {
        let {
          policeAgencyId,
          policeAgencyName,
          activityName,
          declarationPersonnel,
          mobile,
          hostUnit,
          beginTime,
          endTime,
          activityObject,
          activityNumber,
          lng,
          lat,
        } = this.form;

        if (!policeAgencyName) {
          Toast("请选择派出所");
          return;
        }

        if (!activityName || !uniTrim(activityName)) {
          Toast("请输入活动名称");
          return;
        }

        // let companyRegular = /^[a-zA-Z\u4e00-\u9fa5]+$/;
        // if (!companyRegular.test(activityName)) {
        //   Toast("请输入正确的活动名称");
        //   return;
        // }

        if (!declarationPersonnel || !uniTrim(declarationPersonnel)) {
          Toast("请输入申报人员");
          return;
        }

        if (!mobile || !uniTrim(mobile)) {
          Toast("请输入联系方式");
          return;
        }

        if (mobile && !this.$u.test.mobile(mobile)) {
          Toast("请输入正确的手机号");
          return;
        }

        if (!hostUnit || !uniTrim(hostUnit)) {
          Toast("请输入举办单位");
          return;
        }

        if (!beginTime) {
          Toast("请选择活动开始时间");
          return;
        }

        if (!endTime) {
          Toast("请选择活动结束时间");
          return;
        }

        if (!this.activityLocation) {
          Toast("请选择活动地点");
          return;
        }

        if (!activityObject || !uniTrim(activityObject)) {
          Toast("请输入活动对象");
          return;
        }

        if (!activityNumber || !uniTrim(activityNumber)) {
          Toast("请输入活动人数");
          return;
        }

        if (!isNumber(activityNumber)) {
          Toast("活动人数请输入正整数");
          return;
        }

        if (!this.activityContent || !uniTrim(this.activityContent)) {
          Toast("请输入活动内容");
          return;
        }

        let data = {
          policeAgencyId,
          policeAgencyName,
          activityName,
          declarationPersonnel,
          mobile,
          hostUnit,
          beginTime,
          endTime,
          activityLocation: this.activityLocation,
          activityObject,
          activityNumber,
          activityContent: this.activityContent,
          lng,
          lat,
        };

        activityDeclarationSave(data).then((res) => {
          if (res.code == 200) {
            Toast("保存成功");
            uni.setStorageSync("declarationAct", true);
            navBack();
            return;
          }
          Toast(res.message);
        });
      },
    },
  };
</script>

<style>
  page {
    background-color: #f3f3f3 !important;
    height: 100%;
  }
</style>
<style lang="scss" scoped>
  .newActDeclaration {
    flex: 1;
    background-color: #f3f3f3;

    .newActDeclaration-title {
      position: absolute;
      top: 10rpx;
      left: 6%;
      color: #ffffff;
      font-size: 34rpx;
      font-weight: bold;
    }

    .wrapper {
      position: absolute;
      top: 30rpx;
      left: 50%;
      transform: translateX(-50%);

      .wrapper-item {
        margin-bottom: 2vh;
        padding: 35rpx 30rpx;
        background-color: #ffffff;
        border-radius: 10rpx;
        width: 710rpx;
      }

      .content {
        margin: 0 auto;
        height: calc(100vh - 90px);
        overflow-y: auto;
      }

      /deep/.u-form-item__body__right__content {
        padding: 12rpx 18rpx;
        border-radius: 8rrpx;
        background: #f9f9f9;
      }

      /deep/.u-input__content {
        background: #f9f9f9;
      }

      .seleTime {
        width: 30rpx;
        height: 30rpx;
      }

      .forTitle {
        display: flex;
        position: relative;
        margin-bottom: 15rpx;
        font-size: 30rpx;

        .forReq {
          position: absolute;
          left: -9px;
          color: #f56c6c;
          line-height: 20px;
          font-size: 20px;
          top: 3px;
        }

        .state {
          color: #a1a1a1;
          font-size: 26rpx;
        }
      }

      .text-AreaView {

        /deep/.u-textarea,
        .u-textarea__count {
          background-color: #f9f9f9 !important;
        }

        .u-textarea__field {
          font-weight: 400;
        }

        /deep/ .input-placeholder {
          font-weight: 400;
        }
      }
    }

    .map-form {
      display: flex;

      .map-img {
        color: #2488f5;
        font-size: 30rpx;
        font-weight: 500;

        image {
          display: inline-block;
          vertical-align: text-bottom;
          width: 35rpx;
          height: 35rpx;
        }
      }
    }

    .thispageBtn {
      position: absolute;
      width: 100vw;
      bottom: 0;
    }
  }
</style>